package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.OrderApplyAffirm;
import com.xmy.cultivate.entity.OrderDisPrice;
import com.xmy.cultivate.mapper.OrderApplyAffirmMapper;
import com.xmy.cultivate.service.IOrderApplyAffirmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IOrderApplyService;
import com.xmy.cultivate.service.IOrderDetailService;
import com.xmy.cultivate.service.IOrderDisPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-03-09
 */
@Service
public class OrderApplyAffirmServiceImpl extends ServiceImpl<OrderApplyAffirmMapper, OrderApplyAffirm> implements IOrderApplyAffirmService {


    @Autowired
    @Lazy
    OrderApplyAffirmMapper orderApplyAffirmMapper;

    @Autowired
    @Lazy
    IOrderApplyService iOrderApplyService;

    @Autowired
    @Lazy
    IOrderDetailService iOrderDetailService;

    @Autowired
    @Lazy
    IOrderDisPriceService iOrderDisPriceService;

    /**
     * 添加确认
     * @return
     */
    @Override
    public boolean addAffirm(Long orderId, Integer affirmType, Integer type, String remark, Long createdId){
        /*UpdateWrapper<OrderApply> orderApplyUpdateWrapper = new UpdateWrapper<>();
        orderApplyUpdateWrapper.eq("id", orderId);
        orderApplyUpdateWrapper.set("arrive_affirm", affirmType);
        orderApplyUpdateWrapper.set("affirm_type", affirmType);
        orderApplyUpdateWrapper.set("finance_date", LocalDateTime.now().withNano(0));//确认时间
        iOrderApplyService.update(orderApplyUpdateWrapper);

        UpdateWrapper<OrderDetail> orderDetailUpdateWrapper = new UpdateWrapper<>();
        orderDetailUpdateWrapper.eq("order_id", orderId);
        orderDetailUpdateWrapper.set("arrive_affirm", affirmType);
        iOrderDetailService.update(orderDetailUpdateWrapper);*/

        QueryWrapper<OrderApplyAffirm> orderApplyAffirmQueryWrapper = new QueryWrapper<>();
        orderApplyAffirmQueryWrapper.eq("order_id",orderId);
        orderApplyAffirmQueryWrapper.eq("affirm_type",affirmType);
        orderApplyAffirmQueryWrapper.eq("type",type);
        orderApplyAffirmQueryWrapper.last("limit 1");
        OrderApplyAffirm orderApplyAffirm = this.getOne(orderApplyAffirmQueryWrapper);
        if (orderApplyAffirm == null){
            orderApplyAffirm = new OrderApplyAffirm();
        }
        orderApplyAffirm.setOrderId(orderId);
        orderApplyAffirm.setType(type);//1收入确认，2优惠确认，3小票打印
        orderApplyAffirm.setAffirmType(affirmType);//1校长确认，2财务确认，3会计确认
        orderApplyAffirm.setAffirmDate(LocalDate.now());
        orderApplyAffirm.setRemark(remark);
        orderApplyAffirm.setCreatedId(createdId);
        this.saveOrUpdate(orderApplyAffirm);
        return true;
    }

    /**
     * 优惠确认
     * @param orderId
     * @param affirmType
     * @param type
     * @param remark
     * @param createdId
     * @return
     */
    @Transactional
    @Override
    public boolean addDisAffirm(Long disId,Long orderId, Integer affirmType, Integer type, String remark, Long createdId){
        OrderDisPrice orderDisPrice = iOrderDisPriceService.getById(disId);
        orderDisPrice.setStatus(1);
        iOrderDisPriceService.updateById(orderDisPrice);
        this.addAffirm(orderId,affirmType,type,remark,createdId);
        return true;
    }

    /**
     * 删除确认
     * @param orderId
     * @param affirmType
     * @param type
     * @return
     */
    @Override
    public boolean delAffirm(Long orderId, Integer affirmType, Integer type){
        orderApplyAffirmMapper.delAffirm(orderId,affirmType,type);
        //OrderApplyAffirm orderApplyAffirm = orderApplyAffirmMapper.getOneForNew(orderId,affirmType,type);
        return true;
    }

    @Override
    public boolean delAffirmForType(Long orderId, Integer type){

        OrderApplyAffirm orderApplyAffirm = orderApplyAffirmMapper.getOneForOrderId(orderId,2,1);
        if (orderApplyAffirm!=null){
            orderApplyAffirmMapper.delAffirm(orderId,1,1);
        }else {
            orderApplyAffirmMapper.delAffirmForType(orderId,type);
        }

        //OrderApplyAffirm orderApplyAffirm = orderApplyAffirmMapper.getOneForNew(orderId,affirmType,type);
        return true;
    }

    @Override
    public OrderApplyAffirm getOneForOrderId(Long orderId, Integer affirmType, Integer type){
        return orderApplyAffirmMapper.getOneForOrderId(orderId,affirmType,type);
    }
}
